package com.xudow.app.ui.user;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.xudow.app.R;
import com.xudow.app.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class ActivitiesActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final int REFRESH_COMPLETE = 0x1024;

    private ActivityAdapter activitiesAdapter;

    private ListView activityListView;

    private SwipeRefreshLayout swipeRefreshLayout;

    private List<String> activities;

    private Handler refreshHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    activitiesAdapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activities);

        activityListView = (ListView) findViewById(R.id.data_list);

        activities = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            activities.add("");
        }
        activitiesAdapter = new ActivityAdapter(this, android.R.layout.simple_list_item_1, activities);
        activityListView.setAdapter(activitiesAdapter);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    /**
     * 返回按钮点击事件
     *
     * @param view
     */
    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 下拉更新
     */
    @Override
    public void onRefresh() {
        refreshHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }

    private final class ActivityAdapter extends ArrayAdapter<String> {
        private List<String> questions;
        private Context ctx;

        public ActivityAdapter(Context context, int resource, List<String> objects) {
            super(context, resource, objects);
            questions = objects;
            ctx = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(ctx).inflate(R.layout.item_activity_list, null);
            return convertView;
        }
    }
}
